package com.six.park.parking;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import org.json.JSONObject;
import com.baidu.aip.ocr.AipOcr;
import com.six.park.parking.IN.CompratorByLastModified;




public class OUT {
    //设置APPID/AK/SK
    public static final String APP_ID = "18528754";
    public static final String API_KEY = "eCHUtTjNO1Qpt751a0lwYwHq";
    public static final String SECRET_KEY = "ML9QXSON7V7PdsZr0GNjwfDO70lGtNRG";

    public static String aaa() throws Exception {
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(10000);
        client.setSocketTimeoutInMillis(300000);

        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
        //client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
        //client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
        /* System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");*/

       /* // 调用接口
        String path = "C:\\Users\\lenovo\\Desktop\\timg1JQPK2Y7.jpg";
        JSONObject res = client.basicGeneral(path, new HashMap<String, String>());
        System.out.println(res.toString(2));*/

        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("multi_detect", "true");

        String img = getimg();
        // 参数为本地图片路径
        String image = "/root/six/carimg/out/"+img;
        JSONObject res = client.plateLicense(image, options);
        Object obj = res.get("words_result");
        String[] words = obj.toString().split(",");

       /* res = client.plateLicense(file, options);
        System.out.println(res.toString(2));*/

        String str = words[0];
        String[] list = str.split(":");
        String[] strs = list[1].split("\"");
        String carname = strs[1];
        System.out.println("车牌号为："+carname);

        return carname;



    }
    static class CompratorByLastModified implements Comparator<File> {
        public int compare(File f1, File f2) {
            long diff = f1.lastModified() - f2.lastModified();
            if (diff > 0)
                return -1;//倒序正序控制
            else if (diff == 0)
                return 0;
            else
                return 1;//倒序正序控制
        }
        public boolean equals(Object obj) {
            return true;
        }
    }

    //获取文件夹中的最后一张图片
    public static String getimg() throws Exception{
        //本地文件夹的路径
        File file = new File("/root/six/carimg/out");
        File[] fs = file.listFiles();
        Arrays.sort(fs, new CompratorByLastModified());
        for (int i = 0; i < fs.length; i++) {
            System.out.println(fs[i].getName()+" "+new Date(fs[i].lastModified()).toLocaleString());
        }
        //获取最后一张图片的名字
        System.out.println(fs[0].getName()+" ");
        String car = fs[0].getName().toString();
        return car;
    }
}
